package kaka.utils
{
	import com.greensock.TweenLite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import mx.core.UIComponent;

	/**
	 * 根据鼠标移动自动滑动滚动条
	 **/
	public class MouseUtils
	{
		public static const VERTICAL:String="vertical";
		public static const HORIZONTAL:String="horizontal";
		private static var _motherBox:UIComponent;
		private static var _childBox:UIComponent;
		private static var currentType:String="";
		public function MouseUtils()
		{
		}
		/**
		 * 
		 * 根据鼠标移动自动滑动容器
		 * @author kaka
		 * @param motherBox 母容器
		 * @param childBox 子容器(被滑动的容器)
		 * @param type 滑动类型 (横向/纵向=HORIZONTAL/VERTICAL)
		 * 
		 **/
		public static function autoSlide(motherBox:UIComponent,childBox:UIComponent,type:String):void
		{
			_motherBox=motherBox;
			_childBox=childBox;
			currentType=type;
			_motherBox.addEventListener(MouseEvent.MOUSE_OVER,overAndOutHandler);
			_motherBox.addEventListener(MouseEvent.MOUSE_OUT,overAndOutHandler);
		}
		/**
		 * 
		 * 停止鼠标移动自动滑动容器
		 * @author kaka
		 * @param isStop  (停止自动滑动/启动自动滑动=true/false)
		 * 
		 **/
		public static function stopSlide(isStop:Boolean):void
		{
			if(isStop)
			{
				//停止滑动
				_motherBox.removeEventListener(MouseEvent.MOUSE_OVER,overAndOutHandler);
				_motherBox.removeEventListener(MouseEvent.MOUSE_OUT,overAndOutHandler);
				_motherBox.removeEventListener(Event.ENTER_FRAME,frameHandler);
			}
			else
			{
				//重新启用滑动
				_motherBox.addEventListener(MouseEvent.MOUSE_OVER,overAndOutHandler);
				_motherBox.addEventListener(MouseEvent.MOUSE_OUT,overAndOutHandler);
				_motherBox.addEventListener(Event.ENTER_FRAME,frameHandler);
			}
		}
		private static function overAndOutHandler(e:MouseEvent):void
		{
			if(e.type==MouseEvent.MOUSE_OVER)
			{
				_motherBox.addEventListener(Event.ENTER_FRAME,frameHandler);
			}
			else
			{
				_motherBox.removeEventListener(Event.ENTER_FRAME,frameHandler);
			}
		}
		private static function frameHandler(e:Event):void
		{
			var offset:Number;
			switch(currentType)
			{
				case VERTICAL:
					if(_childBox.height>_motherBox.height)
					{
						offset=-_motherBox.height/_childBox.height;
						TweenLite.to(_childBox,0.8,{y:_motherBox.mouseY/offset+100});
					}
					break;
				case HORIZONTAL:
					if(_childBox.width>_motherBox.width)
					{
						offset=-_motherBox.width/_childBox.width;
						TweenLite.to(_childBox,1.0,{x:_motherBox.mouseX/offset+500});
					}
					break;
			}
			
		}
	}
}